package util;

import java.util.ArrayList;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;

import data.RecipeData;
import data.WineData;

public class XMLConverter {
	public static ArrayList<Document> convertToXML(String search, ArrayList<RecipeData> recs, String editLink){
		
		Namespace defNS = Namespace.getNamespace("http://www.w3.org/2005/Atom");
		Namespace customNS = Namespace.getNamespace("ns1", "http://www.example.org");
		Namespace recpupNS = Namespace.getNamespace("recpup", "http://www.recipepuppy.com/about/api/");
		Namespace snoothNS = Namespace.getNamespace("snooth", "http://api.snooth.com");

		Element feed = new Element("feed",defNS);
		feed.addNamespaceDeclaration(customNS);
		feed.addNamespaceDeclaration(recpupNS);
		feed.addNamespaceDeclaration(snoothNS);
		
		Document myDocument = new Document(feed);
		feed.addContent(makeElement("title", defNS, search));
		feed.addContent(createAuthor("Cristina Guzman", "cgg27@cornell.edu", defNS));
		feed.addContent(createAuthor("Joshua Davis", "jad396@cornell.edu", defNS));
		feed.addContent(createAuthor("Phillip Tischler", "pmt43@cornell.edu", defNS));
		 ArrayList<Document> documents = new ArrayList<Document>();
		 documents.add(myDocument);
		//For each recs add an entry to the feed
		for(RecipeData recipeData : recs){
			documents.add(new Document(createEntry(recipeData, defNS, customNS, recpupNS, snoothNS)));
		}
		
		return documents;
	}

	private static Element createAuthor(String name, String email, Namespace defNS){
		Element author = new Element("author", defNS);
		author.addContent(makeElement("name", defNS, name));
		author.addContent(makeElement("email", defNS, email));
		return author;
	}
	private static Element createEntry(RecipeData recipeData, Namespace defNS, Namespace customNS,
			Namespace recpupNS, Namespace snoothNS){
		Element entryElement = new Element("entry",defNS);
		entryElement.addContent(makeElement("title", defNS, recipeData.getTitle()));
		entryElement.addContent(makeElementwithAttribute("category", defNS,"term", "Food and Wine"));
		Element content = new Element("content",defNS);
		entryElement.addContent(content);
		content.addContent(createPairing(recipeData, customNS, recpupNS, snoothNS));
		return entryElement;
	}
	
	private static Element createPairing(RecipeData recipeData,
			Namespace customNS, Namespace recpupNS, Namespace snoothNS) {
		Element pairing = new Element("pairing",customNS);
		pairing.addContent(getRecipe(recipeData, recpupNS));
		pairing.addContent(getWines(recipeData.getWines(), snoothNS));
		return pairing;
	}

	private static Element getRecipe(RecipeData recipeData, Namespace recpupNS){
		Element recipe = new Element("recipe", recpupNS);
		recipe.addContent(makeElement("title", recpupNS, recipeData.getTitle()));
		recipe.addContent(makeElement("href", recpupNS, recipeData.getHref()));	
		recipe.addContent(makeElement("ingredients", recpupNS, recipeData.getIngredients()));
		
		return recipe;
	}
	
	private static Element getWines(ArrayList<WineData> recWines, Namespace snoothNS){
		Element wines = new Element("wines", snoothNS);
		for(WineData data : recWines){
			wines.addContent(getWine(data, snoothNS));
		}
		return wines;
	}
	
	private static Element getWine(WineData wineData, Namespace snoothNS){
		Element wine = new Element("wine", snoothNS);
		wine.addContent(makeElement("name", snoothNS, wineData.getName()));
		wine.addContent(makeElement("region", snoothNS, wineData.getRegion()));	
		wine.addContent(makeElement("winery", snoothNS, wineData.getWinery()));
		wine.addContent(makeElement("varietal", snoothNS, wineData.getVarietal()));
		wine.addContent(makeElement("price", snoothNS, wineData.getPrice()));
		wine.addContent(makeElement("vintage", snoothNS, wineData.getVintage()));
		wine.addContent(makeElement("type", snoothNS, wineData.getDescrip()));
		wine.addContent(makeElement("link", snoothNS, wineData.getUrl()));
		wine.addContent(makeElement("snoothrank", snoothNS, wineData.getRating()));
		wine.addContent(makeElement("image", snoothNS, wineData.getImageURL()));
		
		return wine;
	}
	
	private static Element makeElement(String name, Namespace ns, String textContent){
		Element element = new Element(name, ns);
		element.addContent(textContent);
		return element;
	}
	
	private static Element makeElementwithAttribute(String name, Namespace ns, String attribute, String textContent){
        Element element = new Element(name, ns);
        element.setAttribute( attribute, textContent );
        return element;
    }
}
